home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / watcom / llcomm13 / lcr.c < prev    next >
C/C++ Source or Header  |  1994-08-26  |  9KB  |  357 lines

  1. /*
  2. ** LCR.C   Communication Port Test Code
  3. **
  4. ** Coded by : James P. Ketrenos
  5. **
  6. **     You have permission to use this if you want.  You are not required
  7. **     to use it.  However, if it should cause harm to you, your computer,
  8. **  your dog, or anyone/thing you know/have/own, I am not responsible.
  9. **
  10. **  By using this, or any file in this archive, you acknowledge that
  11. **  fact and accept it.
  12. **
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <conio.h>
  17. #include "ll_comm.h"
  18. #include "ll_stat.h"
  19.  
  20. #define VER_ID  "0001.3000"
  21.  
  22. int     COM_BASE=0x3F8;
  23. int     COM_IRQ=4;
  24. int     COM_RATE=57600;
  25. int     COM_SET=(BITS_8 | STOP_1 | NO_PARITY);
  26. int     COM_FIFO=0;
  27. COMM    Port;
  28.  
  29. int     ECHO=0;
  30. int     LF=0;
  31. int     xpos,ypos=0;
  32. char    *screen=(char *)0x000b8000;
  33.  
  34. ClearIO()
  35. {
  36. int x;
  37.     for (x=0; x<80*25*2; x+=2)
  38.     {
  39.         screen[x]=0;
  40.         screen[x+1]=7;
  41.     }
  42.     xpos=ypos=0;
  43. }
  44.  
  45. PrintIO(char    key)
  46. {
  47.     switch (key)
  48.     {
  49.     case    9:xpos+=10;
  50.             if (xpos>160)
  51.             {
  52.                 xpos=0;
  53.                 ypos++;
  54.                 if (ypos>24)
  55.                 {
  56.                     ClearIO();
  57.                     ypos=0;
  58.                 }
  59.             }
  60.             break;
  61.     case    8:xpos-=2;
  62.             if (xpos<0) xpos=0;
  63.             screen[ypos*160+xpos]=0;
  64.             break;
  65.     case    13:xpos=0;
  66.             break;
  67.     case    10:ypos++;
  68.             if (ypos>24)
  69.             {
  70.                 ClearIO();
  71.                 ypos=0;
  72.             }
  73.             break;
  74.     default:screen[ypos*160+xpos]=key;
  75.             xpos+=2;
  76.             if (xpos>160)
  77.             {
  78.                 xpos=0;
  79.                 ypos++;
  80.                 if (ypos>24)
  81.                 {
  82.                     ClearIO();
  83.                     ypos=0;
  84.                 }
  85.             }
  86.     }
  87. }
  88.  
  89. WriteStr(COMM Port, char    *str)
  90. {
  91.     for (; *str; str++)
  92.         ioWriteByte(Port, *str);
  93. }
  94.  
  95. PrintStr(char    *str)
  96. {
  97.     for (; *str; str++)
  98.         PrintIO(*str);
  99. }
  100.  
  101. int    main(int argc, char *args[])
  102. {
  103. int     key,count,prior;
  104. int     COM_TEMP;
  105. int     Escape=0;
  106.  
  107.     status(ST_RESET,"LCR.LOG");
  108.     status(ST_ECHO,"");
  109.     status(ST_SHOW,"[ LCR (R) Logical Comm Routines v%s ]",VER_ID);
  110.     status(ST_ECHO,"[ Copyright (C) 1994  by J.P.K. ]");
  111.     status(ST_ECHO,"");
  112.     status(ST_ECHO,"See LCR.NFO for important information.");
  113.     status(ST_SHOW,"");
  114.  
  115.     for (count=1; count<argc; count++)
  116.     {
  117.         if (args[count][0]!='-' && args[count][0]!='/')
  118.         {
  119.             status(ST_SHOW,"Bad command line.  -? for help.");
  120.             exit(-1);
  121.         }
  122.         switch (args[count][1])
  123.         {
  124.         case    0:
  125.         case    '?':printf("Usage:  LCR [ -[ [[?pbifBS]#] [P[NOEMS]] ] ]\n");
  126.                     printf("Where:  p       = Port (HEX)    (3E8, 2E8, etc)         [3E8]\n");
  127.                     printf("        b       = Baud (DEC)    (1 - 115200)            [57600]\n");
  128.                     printf("        i       = IRQ  (DEC)    (0 - 7)                 [4]\n");
  129.                     printf("        f       = FIFO Buffer   (0,1,4,8,14)            [0]\n");
  130.                     printf("        B       = Bits Per Word (5-8)                   [8]\n");
  131.                     printf("        S       = Stop Bits     (1-2)                   [1]\n");
  132.                     printf("        P       = Parity                                [NONE]\n");
  133.                     printf("                : None, Odd, Even, Mark, Space\n");
  134.                     printf("\n");
  135.                     printf("NOTE:      5 Bits with 1 Stop Bit *may* not work with your\n");
  136.                     printf("        hardware.\n");
  137.                     exit(0);
  138.         case    'p':sscanf(&args[count][2],"%x",&COM_BASE);
  139.                     printf("Port set to: 0x%X\n",COM_BASE);
  140.                     break;
  141.         case    'b':sscanf(&args[count][2],"%d",&COM_RATE);
  142.                     if (COM_RATE < 1 || COM_RATE > 115200)
  143.                     {
  144.                         status(ST_SHOW,"Invalid Baud Rate.  Valid Range 1 - 115200.");
  145.                         exit(-1);
  146.                     }
  147.                     printf("Baud set to: %d\n",COM_RATE);
  148.                     break;
  149.         case    'i':sscanf(&args[count][2],"%d",&COM_IRQ);
  150.                     if (COM_IRQ&!0x07)
  151.                     {
  152.                         status(ST_SHOW,"Invalid IRQ.  Valid Range 0 - 7.");
  153.                         exit(-1);
  154.                     }
  155.                     printf("IRQ  set to: %d\n",COM_IRQ);
  156.                     printf("Interrupt  : %d\n",COM_IRQ+8);
  157.                     break;
  158.         case    'f':sscanf(&args[count][2],"%d",&COM_FIFO);
  159.                     switch (COM_FIFO)
  160.                     {
  161.                     case    0:case      1:case    4:case      8:case    14:
  162.                             break;
  163.                     default:status(ST_SHOW,"Invalid FIFO.  Valid Values (0,1,4,8,14).");
  164.                             exit(-1);
  165.                     }
  166.                     printf("FIFO  set to: %d\n",COM_FIFO);
  167.                     break;
  168.         case    'B':sscanf(&args[count][2],"%d",&COM_TEMP);
  169.                     switch (COM_TEMP)
  170.                     {
  171.                     case    5:case      6:case    7:case      8:
  172.                             COM_SET&=CLEAR_BITS;
  173.                             COM_SET|=COM_TEMP-5;
  174.                             break;
  175.                     default:status(ST_SHOW,"Invalid BIT Size.  Valid Range 5 - 8.");
  176.                             exit(-1);
  177.                     }
  178.                     printf("Bits per word set to: %d\n",COM_TEMP);
  179.                     break;
  180.         case    'S':sscanf(&args[count][2],"%d",&COM_TEMP);
  181.                     switch (COM_TEMP)
  182.                     {
  183.                     case    1:case      2:
  184.                             COM_SET&=CLEAR_STOP;
  185.                             COM_SET|=(COM_TEMP-1)<<2;
  186.                             break;
  187.                     default:status(ST_SHOW,"Invalid STOP Count.  Valid Range 1 - 2.");
  188.                             exit(-1);
  189.                     }
  190.                     printf("Stop Bits set to: %d\n",COM_TEMP);
  191.                     break;
  192.         case    'P':COM_TEMP=(int)args[count][2];
  193.                     switch (COM_TEMP<'a' ? COM_TEMP-'A'+'a' : COM_TEMP)
  194.                     {
  195.                     case    'n':
  196.                             COM_SET&=CLEAR_PARITY;
  197.                             printf("Parity set to: NONE\n");
  198.                             break;
  199.                     case    'o':
  200.                             COM_SET&=CLEAR_PARITY;
  201.                             COM_SET|=ODD_PARITY;
  202.                             printf("Parity set to: ODD\n");
  203.                             break;
  204.                     case    'e':
  205.                             COM_SET&=CLEAR_PARITY;
  206.                             COM_SET|=EVEN_PARITY;
  207.                             printf("Parity set to: EVEN\n");
  208.                             break;
  209.                     case    'm':
  210.                             COM_SET&=CLEAR_PARITY;
  211.                             COM_SET|=MARK_PARITY;
  212.                             printf("Parity set to: MARK\n");
  213.                             break;
  214.                     case    's':
  215.                             COM_SET&=CLEAR_PARITY;
  216.                             COM_SET|=SPACE_PARITY;
  217.                             printf("Parity set to: SPACE\n");
  218.                             break;
  219.                     default:status(ST_SHOW,"Invalid PARITY.  Valid types:");
  220.                             status(ST_SHOW,"    None, Odd, Even, Mark, Stop");
  221.                             exit(-1);
  222.                     }
  223.                     break;
  224.         default:    status(ST_SHOW,"Bad command line.  -? for help.");
  225.                     exit(-1);
  226.         }
  227.     }
  228.  
  229.     printf("\n");
  230.     status(ST_WRITE,"[ Command Line Parsed ]");
  231.     status(ST_WRITE,"Base Port        : 0x%X",COM_BASE);
  232.     status(ST_WRITE,"Port IRQ         : %d",COM_IRQ);
  233.     status(ST_WRITE,"Port Interrupt   : 0x%X",COM_IRQ+8);
  234.     status(ST_WRITE,"FIFO Buffer      : %d",COM_FIFO);
  235.     status(ST_WRITE,"Baud Rate        : %d",COM_RATE);
  236.     status(ST_WRITE,"Control Settings : 0x%X",COM_SET);
  237.     status(ST_WRITE,"");
  238.  
  239.     switch(COM_BASE)
  240.     {
  241.     case    0x3E8:case 0x3F8:case 0x2E8:case 0x2F8:
  242.             break;
  243.     default:    status(ST_SHOW,"WARNING:  0x%X unrecognized as standard port base.",COM_BASE);
  244.                 printf("Are you sure you wish to use it? (y/N)\n");
  245.                 switch (getche())
  246.                 {
  247.                 case    'Y':case 'y':
  248.                         break;
  249.                 default:printf("\nExiting...\n");
  250.                         exit(-1);
  251.                 }
  252.                 printf("\n");
  253.                 while (kbhit()) getch();
  254.     }
  255.  
  256.     status(ST_SHOW,"[ Initializing Port ]");
  257.     Port=ioOpenPort(COM_BASE, COM_IRQ);
  258.     if(!Port)
  259.     {
  260.         status(ST_SHOW,"Error initalizing port.\n");
  261.         return    1;
  262.     }
  263.     status(ST_WRITE,"Latched Port ID at (struct PortID *)0x%X",Port);
  264.     status(ST_WRITE,"");
  265.  
  266.     ioSetBaud(Port, COM_RATE);
  267.     ioSetHandShake(Port, DTR | RTS);
  268.     ioSetControl(Port, COM_SET);
  269.     ioSetMode(Port, BYTE_MODE);
  270.  
  271.     ClearIO();
  272.     PrintStr("Initializing Modem . . .\n\r");
  273.     WriteStr(Port, "AT&F\r");
  274.  
  275.     while (!Escape)
  276.     {
  277.         while (!kbhit() && !ioReadStatus(Port));
  278.         if (kbhit())
  279.             switch (key=getch())
  280.             {
  281.             case    27: Escape=1;
  282.                         break;
  283.             case    0:case 0xE0:
  284.                     if (kbhit())
  285.                         switch (key=getch())
  286.                         {
  287.                         case    0x12:ECHO=1-ECHO;
  288.                                     PrintStr("\n\r[ECHO ");
  289.                                     PrintStr(ECHO ? "ON" : "OFF");
  290.                                     PrintStr("]\n\r");
  291.                                     break;
  292.                         case    0x26:LF=1-LF;
  293.                                     PrintStr("\n\r[LF ");
  294.                                     PrintStr(LF ? "ON" : "OFF");
  295.                                     PrintStr("]\n\r");
  296.                                     break;
  297.                         case    0x17:WriteStr(Port,"AT&F\r");
  298.                                     break;
  299.                         case    0x3B:PrintStr("\n\r[ LCR:  Help ]\n\r");
  300.                                     PrintStr("ALT-E     Toggle ECHO ON/OFF\n\r");
  301.                                     PrintStr("ALT-I     Initialize Modem\n\r");
  302.                                     PrintStr("ALT-L     Toggle LineFeeds\n\r");
  303.                                     PrintStr("F1        Help\n\r");
  304.                                     PrintStr("F2        Clear Screen\n\r");
  305.                                     PrintStr("F3        Cool message\n\r");
  306.                                     PrintStr("F4        Dial DeadMan' Hand\n\r");
  307.                                     PrintStr("F5        Dial PSU #1\n\r");
  308.                                     PrintStr("F6        Dial PSU #2\n\r");
  309.                                     PrintStr("ESC       Exit\n\r");
  310.                                     break;
  311.                         case    0x3C:ClearIO(); break;
  312.                         case    0x3D:PrintStr("\n\r[LL is ");
  313.                                     PrintStr(ECHO ? "cool" : "neato");
  314.                                     PrintStr("]\n\r");
  315.                                     break;
  316.                         case    0x3E:WriteStr(Port, "ATDT9,2889264\r");
  317.                                     break;
  318.                         case    0x3F:WriteStr(Port,"ATDT54206\r");
  319.                                     break;
  320.                         case    0x40:WriteStr(Port,"ATDT53144\r");
  321.                                     break;
  322.                         default:    break;
  323.                         }
  324.                     break;
  325.             default:if (ECHO)
  326.                         PrintIO(key);
  327.                     ioWriteByte(Port, key);
  328.                     break;
  329.             }
  330.         if (ioReadStatus(Port))
  331.             switch (key=ioReadByte(Port))
  332.             {
  333.             case    27: while (!kbhit() && !ioReadStatus(Port));
  334.                         if (ioReadByte(Port))
  335.                         {
  336.                             if (ioReadByte(Port)=='[')
  337.                             {
  338.                                 PrintIO('E');
  339.                                 PrintIO('[');
  340.                             }
  341.                         }
  342.                         break;
  343.             case    13: if (prior!=10)
  344.                         PrintIO(10);
  345.                         PrintIO(prior=13);
  346.                         break;
  347.             default:PrintIO(key);
  348.                     prior=key;
  349.                     break;
  350.             }
  351.     }
  352.     status(ST_SHOW,"[ Reseting Port ]");
  353.     status(ST_WRITE,"");
  354.     ioClosePort(Port);
  355.     return    0;
  356. }
  357.